using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;

public partial class VerifyAD : System.Web.UI.Page
{
    string[] ActiveDirectoryAddress = new string[2];

    protected void Page_Load(object sender, EventArgs e)
    {
        
        this.Table_MessageBox.Visible = false;

        //ActiveDirectoryAddress[0] = GonzalesGeneralVariables.LDAP;
        //ActiveDirectoryAddress[1] = GonzalesGeneralVariables.AlternateLDAP1;
        ActiveDirectoryAddress[0] = "tpsing-dc01/OU=Users,OU=AP,DC=THCG,DC=NET";

    }

    protected void Button_Test_Click(object sender, EventArgs e)
    {
        bool success = false;

        string ActiveDirectoryID = this.TextBox_ADID.Text;
        string ActiveDirectoryPassword = this.TextBox_ADPWD.Text;

        DirectoryEntry CurADSystem = null;
        DirectorySearcher CurADSearcher = null;
        SearchResult CurSearchResult = null;

        foreach (string LDAP_Address in ActiveDirectoryAddress)
        {
            try
            {
                CurADSystem = new DirectoryEntry(LDAP_Address, ActiveDirectoryID, ActiveDirectoryPassword);
                CurADSearcher = new DirectorySearcher(CurADSystem);
                CurADSearcher.Filter = "samaccountname=" + ActiveDirectoryID;
                CurADSearcher.SearchScope = SearchScope.Subtree;
                CurSearchResult = CurADSearcher.FindOne();

                if (CurSearchResult != null)
                {
                    // success
                    // now get more info
                    string MoreInfo = "<p>Login Success at " + LDAP_Address + "</p>";

                    MoreInfo += "<ul>";

                    ResultPropertyCollection CurResultProperties = CurSearchResult.Properties;

                    foreach (string PropertyKey in CurResultProperties.PropertyNames)
                    {
                        switch (PropertyKey)
                        {

                            case "manager":

                                string DirectManagerID = "";
                                string ManagerEmail = "";

                                foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                {
                                    DirectManagerID += TmpCollection.ToString();
                                }

                                #region Get Manager Info

                                DirectoryEntry CurManagerADSystem = new DirectoryEntry(LDAP_Address, ActiveDirectoryID, ActiveDirectoryPassword);
                                DirectorySearcher ManagerSearcher = new DirectorySearcher(CurManagerADSystem);
                                ManagerSearcher.Filter = "distinguishedname=" + DirectManagerID;
                                ManagerSearcher.SearchScope = SearchScope.Subtree;
                                SearchResult ManagerSearchResult = ManagerSearcher.FindOne();

                                if (ManagerSearchResult != null)
                                {
                                    ResultPropertyCollection CurManagerResultProperties = ManagerSearchResult.Properties;
                                    foreach (string ManagerPropertyKey in CurManagerResultProperties.PropertyNames)
                                    {
                                        if (ManagerPropertyKey.CompareTo("mail") == 0)
                                        {
                                            foreach (Object TmpCollection in CurManagerResultProperties[ManagerPropertyKey])
                                            {
                                                ManagerEmail += TmpCollection.ToString();
                                            }
                                        }
                                    }
                                }

                                #endregion

                                MoreInfo += "<li>Reporting to: " + DirectManagerID + " (" + ManagerEmail + ")</li>";                                
                                break;

                            case "distinguishedname":

                                string ADDistingushedName = "";
                                foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                {
                                    ADDistingushedName += TmpCollection.ToString();
                                }

                                MoreInfo += "<li>AD DistinguishedName: " + ADDistingushedName + "</li>";
                                break;

                            case "mail":

                                string Email = "";
                                foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                {
                                    Email += TmpCollection.ToString();
                                }

                                MoreInfo += "<li>Email: " + Email + "</li>";
                                break;
                        }
                    }
                    
                    MoreInfo += "</ul>";

                    success = true;
                    this.ShowMessage(MoreInfo);
                    break;
                }
            }
            catch (Exception ex)
            {
                this.ShowErrorMessage("Exception: " + ex.Message);
            }
        }

        if (!success)
        {
            this.ShowErrorMessage("Login fail");
        }

    }

    #region ShowErrorMessage
    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "ErrorBox";
    }
    #endregion

    #region ShowMessage
    protected void ShowMessage(string Message)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = Message;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "MessageBox";
    }
    #endregion
}
